<template>
  <el-dialog
    :visible="isShow"
    title="新增员工"
    width="600px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="1~30个字符" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" placeholder="选择日期" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employeesDate.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="1~20个字符" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <div ref="department" v-clickOut="clickOutEvent">
          <el-input
            v-model="form.departmentName"
            readonly
            placeholder="1~30个字符"
            @focus="focusEvent"
          />
          <!-- 部门展示 -->
          <el-tree
            v-if="showTree"
            :data="treeDate"
            :props="{
              label: 'name',
              children: 'children'
            }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" placeholder="选择日期" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { companyDepartment } from '@/api/department'
import employeesDate from '@/api/constant/employees'
import { sysUserPost } from '@/api/employees'

export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employeesDate,
      treeDate: [],
      showTree: false,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 30,
            message: '请正确填写姓名',
            trigger: 'change'
          }
        ], //	string	非必须		姓名
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ], //	string	非必须		手机号
        timeOfEntry: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	string	非必须		入职时间
        formOfEmployment: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	number	非必须		聘用形式
        workNumber: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	string	非必须		工号
        departmentName: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	string	非必须		组织名称
        correctionTime: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ] //	string	非必须		转正时间
      }
    }
  },
  mounted() {
    // document.addEventListener('click', (e) => {
    //   // 判断点击的dom是不是树形控件区域,如果不是关闭树形控件,如果是不用理
    //   // department的dom区域: this.$refs.department
    //   // 点击的dom: e.target
    //   if (!this.$refs.department.contains(e.target)) {
    //     this.showTree = false
    //   }
    // })
  },
  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.$emit('getData')
          this.closeEvent()
        }
      })
    },
    async focusEvent() {
      this.showTree = true
      if (this.treeDate.length === 0) {
        const res = await companyDepartment()
        this.treeDate = this.changeData(res.data.depts)
        console.log('组织架构', res)
      }
    },
    // 转换
    changeData(arr, pid = '') {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 组织架构点击处理
    nodeClick(data) {
      this.showTree = false
      this.form.departmentName = data.name
    },
    clickOutEvent() {
      this.showTree = false
    }
  }
}
</script>

<style></style>
